﻿using BW.IBLL;
using BW.IDAL;
using BW.Models;
using MD5Hash;

namespace BW.BLL
{
    public class EmployeeBLL : IEmployeeBLL
    {

        public EmployeeBLL(IBaseDAL<Employee> employeeDAL)
        {
            EmployeeDAL = employeeDAL;
        }

        public IBaseDAL<Employee> EmployeeDAL { get; }

        public async Task<int> AddEmployee(Employee employee)
        {
            //名称唯一
            bool exitis = EmployeeDAL.GetAllAsync().Any(x => x.Name == employee.Name);
            if (exitis)
            {
                return -1;
            }
            else
            {
                employee.Password = employee.Password.GetMD5();
                return await EmployeeDAL.AddAsync(employee);
            }
            
        }

        public Employee Login(string name, string pwd)
        {
            Employee emp = EmployeeDAL.GetAllAsync().FirstOrDefault(x => x.Name == name);
            if (emp == null) {
                emp = new Employee { Id=-1 };   
            }
            else
            {
                if (emp.Password != pwd.GetMD5()) {
                    emp = new Employee { Id = -2 };
                }
            }
            return emp;
        }
    }
}
